package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.TActivityUser;
import com.ruoyi.system.service.ITActivityUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TActivityLove;
import com.ruoyi.system.service.ITActivityLoveService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动管理Controller
 * 
 * @author yyh
 * @date 2024-06-21
 */
@RestController
@RequestMapping("/system/love")
public class TActivityLoveController extends BaseController
{
    @Autowired
    private ITActivityLoveService tActivityLoveService;

    @Autowired
    private ITActivityUserService itActivityUserService;

    /**
     * 查询活动管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:love:list')")
    @GetMapping("/list")
    public TableDataInfo list(TActivityLove tActivityLove)
    {
        startPage();
        List<TActivityLove> list = tActivityLoveService.selectTActivityLoveList(tActivityLove);
        if(tActivityLove.getUserId()!=null&&tActivityLove.getUserId()!=0){
            TActivityUser activityUser = new TActivityUser();
            activityUser.setUserId(tActivityLove.getUserId());
            List<Long> activityList = new ArrayList<>();
            for(TActivityLove love : list){
                activityList.add(love.getId());
            }
            activityUser.setActivityIdList(activityList);
            //查找该列表下的人员记录
            List<TActivityUser> userList = itActivityUserService.selectTActivityUserList(activityUser);
            if(!userList.isEmpty()){
                for(TActivityLove love : list){
                    for(TActivityUser user : userList){
                        if(love.getId().equals(user.getActivityId())){
                            love.setFlag(true);
                            break;
                        }
                    }
                }
            }

        }
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:love:list')")
    @GetMapping("/listUser")
    public TableDataInfo listUser(TActivityLove tActivityLove)
    {
        startPage();
        List<TActivityLove> list = tActivityLoveService.selectTActivityUserList(tActivityLove);
        return getDataTable(list);
    }

    /**
     * 导出活动管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:love:export')")
    @Log(title = "活动管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TActivityLove tActivityLove)
    {
        List<TActivityLove> list = tActivityLoveService.selectTActivityLoveList(tActivityLove);
        ExcelUtil<TActivityLove> util = new ExcelUtil<TActivityLove>(TActivityLove.class);
        util.exportExcel(response, list, "活动管理数据");
    }

    /**
     * 获取活动管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:love:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tActivityLoveService.selectTActivityLoveById(id));
    }

    /**
     * 新增活动管理
     */
    @PreAuthorize("@ss.hasPermi('system:love:add')")
    @Log(title = "活动管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TActivityLove tActivityLove)
    {
        return toAjax(tActivityLoveService.insertTActivityLove(tActivityLove));
    }

    /**
     * 修改活动管理
     */
    @PreAuthorize("@ss.hasPermi('system:love:edit')")
    @Log(title = "活动管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TActivityLove tActivityLove)
    {
        return toAjax(tActivityLoveService.updateTActivityLove(tActivityLove));
    }

    /**
     * 删除活动管理
     */
    @PreAuthorize("@ss.hasPermi('system:love:remove')")
    @Log(title = "活动管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tActivityLoveService.deleteTActivityLoveByIds(ids));
    }
}
